;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Account.RepaymentDetails", [])
        .controller("RepaymentDetailsCtrl", ["$scope", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", "$timeout", "$filter", function ($scope, ybWidget, ybHttp, MESG_TYPE, UserContextService, $timeout, $filter) {
            $scope.activeFlag = [true, false];
            $scope.markInfo = {
                allChecked: false
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.toolbarConfig = {};
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                    return;
                }
                $timeout(function () {
                    $scope.toolbarConfig["show" + toolbar] = true;
                });
            };
            $scope.reset = function () {
                $scope.condition = {};
            };
            $scope.tableConfig = {
                fixedWidth: 40,
                scrollWidth: 1800,
                tableCol: [
                    {
                        name: "经销商名称",
                        dataField: "dealerName",
                        width: 160,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "经销商编码",
                        dataField: "dealerCode",
                        width: 120,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "发货通知单号",
                        dataField: "deliveryNo",
                        width: 160,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "采购订单号",
                        dataField: "purchaseNo",
                        width: 160,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "零件号",
                        dataField: "materialsNo",
                        width: 120,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "零件中文名",
                        dataField: "materialsDes",
                        width: 160,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "数量",
                        dataField: "qty",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "单价",
                        dataField: "price",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "总价",
                        dataField: "totalPrice",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "零件类型",
                        dataField: "materialsType",
                        width: 140,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "已还金额",
                        dataField: "repayAmount",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "未还金额",
                        dataField: "remainAmount",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "年月",
                        dataField: "createDateShow",
                        width: 140,
                        isShow: true,
                        isFixed: false
                    },
                    /* {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 80,
                        isShow: true,
                        isFixed: true
                    } */
                ]
            };
            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    billFixedWidth: {left: $scope.tableConfig.fixedWidth + "px"}
                };
            };

            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/repay-detail-record/export"),
                sectionParam: {ids: []},
                allUrl: ybHttp.RequestUrl("device", "/repay-detail-record/exportAll")
            };

            $scope.detailInfo = {};
            $scope.dataList = [];
            $scope.condition = {};

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.changeTab = function (index) {
                if (index === 0) {
                    $scope.detailInfo = {};
                }
                angular.forEach($scope.activeFlag, function (activeFlag) {
                    activeFlag = false;
                });
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.selectAll = function () {
                for (var i = 0; i < $scope.dataList.length; i++) {
                    $scope.dataList[i].isChecked = $scope.markInfo.allChecked;
                }
                getBillIds();
            };

            $scope.selectSingle = function (item) {
                var selectAllFlag = false;
                if (!item.isChecked) {
                    selectAllFlag = false;
                } else {
                    selectAllFlag = checkAll($scope.dataList);
                }
                $scope.markInfo.allChecked = selectAllFlag;
                getBillIds();
            };
            function checkAll(list) {
                var selectAllFlag = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAllFlag = false;
                        break;
                    }
                    selectAllFlag = true;
                }
                return selectAllFlag;
            }
            function getBillIds() {
                $scope.exportParam.sectionParam.ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.ids.push(item.id);
                    }
                });
            }

            $scope.search = function (curPage) {
                $scope.markInfo.allChecked = false;
                if (curPage) {
                    $scope.pageInfo.CurPage = curPage;
                }
                getDataList();
            };

            function getDataList () {
                var url = '/repay-detail-record/list';
                var params = {
                    filters: $scope.condition,
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage
                };
                ybHttp.Post('device', url, params).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(res.value.list, function (item) {
                        item.isChecked = false;
                        item.createDateShow = $filter("YbDate")(item.createDate, 'yyyy-MM');
                    });
                    $scope.pageInfo.totalRows = res.value.total;
                    $scope.pageInfo.totalPages = res.value.pages;
                    $scope.dataList = res.value.list;
                })
            }

            var btnPermissionCode = {
                prePayExport: window.ActionCode.prePayExport,
                prePayReverse: window.ActionCode.prePayReverse,
                prePayAdd: window.ActionCode.prePayAdd
            };
            $scope.btnPermission = {
                prePayExport: true,
                prePayReverse: true,
                prePayAdd: true
            };
            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            // 初始化页面信息
            (function () {
                getDataList();
                UserContextService.SetBtnPermission(setBtnPermission);
                $scope.setFixedWidth();
            })()
        }])
    ;
})(window, window.angular);